<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * Created on 2012-7-3
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class Main extends MY_Controller{

 	private $_uri = '';
 	private $_current_page = 0;
 	private $_limit = 5;
 	private $_offset =0;
 	private $_articles = array();
 	private $_pageinfo = '';
	private $_total_count = 0;
 	function __construct(){
 		parent::__construct();

 	}
	/*
	 * 根据分类和page也进行查询，默认为所有分类和第一页
	 */
 	function index($category ='all',$page = 1,$author=''){
 		$this->_init_page($page);
 		$this->load->model('articles_m');
 		$this->_articles = $this->articles_m->get_by_page($category,$author,$this->_offset,$this->_limit);
 		$this->num_rows = $this->_articles->num_rows();
 		$this->_articles = $this->_articles->result();
		//分页设置
		$this->load->library('pagination');
		if($author!=''){
			if($category !='all'){
				$config['base_url'] = base_url('/index.php/author/'.$author.'/'.$category).'/';
				$config['uri_segment'] = 4;
			}else{
				$config['base_url'] = base_url('/index.php/author/'.$author).'/';
				$config['uri_segment'] = 3;
			}
		}else{
			$config['base_url'] = base_url('/index.php/category/'.$category).'/';
			$config['uri_segment'] = 3;
		}
		$config['total_rows'] = $this->articles_m->get_count_by_page($category,$author);
		$config['per_page'] = $this->_limit;
		$config['use_page_numbers'] = TRUE;
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->javascript->compile();
		$this->pagination->initialize($config);
		$data['articles'] = $this->_articles;
		$data['num_rows']= $this->num_rows;
		$data['pageinfo'] = $this->pagination->create_links();
		$this->load_theam_view('index',$data);

 	}

 	/*
 	 * 初始化当前页以及$limit 和 offset。
 	 */
	private function _init_page($currentpage){
		$this->_current_page = ($currentpage && is_numeric($currentpage)) ?intval($currentpage) :1 ;
		/** 获取用户自定义每页多少项 */
		$page_size = common::_get_setting('posts_page_size');
		$this->_limit = ($page_size && is_numeric($page_size)) ? intval($page_size) : $this->_limit;
		$this->_offset = ($this->_current_page-1)*$this->_limit;
		//获取当前类别id号
		//$category = $this->_category($this->uri->segment('2'));
		//var_dump($category);
	}

	/*获取类别及父类信息
	 * @param $category_alias :类别的别名
	 * @return array()
	 * */
	private function _category($category_alias){
		static $category_info ;
		$category_alias = $category_alias ? $category_alias : 'all';
		$category_info[$category_alias] = $this->db->get_where('category',array('alias'=>$category_alias))->result();
		$fid = $category_info[$category_alias][0]->fid;
		if($fid !=0){
			$falias = $this->db->get_where('category',array('id'=>$fid))->result();
			$this->_category($falias[0]->alias);
		}
		return $category_info;
	}


 }
?>
